'use strict';
/**
 * Created by FreezeSoul on 2015/10/28.
 */
define([
        'angular',
        'app/core/editor'
    ],
    function(angular) {
        angular.module('ad.editor').controller(
            "adBarPluginEditorController",['$rootScope','$scope','_','Notification',function($rootScope,$scope,_,Notification) {

                $scope.colors = [];

                $scope.CustomColor = false;

                if($scope.extendOfIndex.SeriesColors){
                    $scope.CustomColor = true;
                    $scope.colors = $scope.extendOfIndex.SeriesColors;
                }else{
                    $scope.CustomColor = false;
                    $scope.colors = angular.copy(adConfig.colorArray);
                }

                $scope.addColor = undefined;

                $scope.customColor = function () {
                    if(!$scope.CustomColor){
                        delete $scope.extendOfIndex.SeriesColors;
                        $scope.colors = angular.copy(adConfig.colorArray);
                    }else{
                        $scope.extendOfIndex.SeriesColors = $scope.colors;
                    }
                };
                
                $scope.removeColor = function (color) {
                    var index = $scope.extendOfIndex.SeriesColors.indexOf(color);
                    if (index > -1) {
                        $scope.extendOfIndex.SeriesColors.splice(index, 1);
                    }
                };

                $scope.$on("ad.index.option.changed", function (event, args) {
                    if($scope.extendOfIndex.SeriesColors){
                        $scope.CustomColor = true;
                        $scope.colors = $scope.extendOfIndex.SeriesColors;
                    }else{
                        $scope.CustomColor = false;
                        $scope.colors = angular.copy(adConfig.colorArray);
                    }
                });

                $scope.$on("colorpicker-closed", function (event, args) {
                    if(!$scope.addColor) return;
                    if(!$scope.CustomColor) return;
                    if(args.name != "addColor") return;
                    var index = $scope.extendOfIndex.SeriesColors.indexOf($scope.addColor);
                    if (index == -1) {
                        $scope.extendOfIndex.SeriesColors.push($scope.addColor);
                        $scope.$digest();
                    }
                });


                $scope.positionArray = [
                    {value:'left,top',text:'左上'},
                    {value:'center,top',text:'中上'},
                    {value:'right,top',text:'右上'},
                    {value:'left,center',text:'左中'},
                    {value:'center,center',text:'正中'},
                    {value:'right,center',text:'右中'},
                    {value:'left,bottom',text:'左下'},
                    {value:'center,bottom',text:'中下'},
                    {value:'right,bottom',text:'右下'}
                ];

                $scope.valueWhereArray = [
                    {value:'yAxis',text:'Y轴'},
                    {value:'xAxis',text:'X轴'}
                ];

                $scope.gridShowArray = [
                    {value:'yAxis&xAxis',text:'X轴&Y轴显示'},
                    {value:'yAxis',text:'仅Y轴显示'},
                    {value:'xAxis',text:'仅X轴显示'},
                    {value:'None',text:'不显示'}
                ];

                $scope.seriesTypeArray = [
                    {value:'bar',text:'Bar'},
                    {value:'line',text:'Line'},
                    {value:'scatter',text:'Scatter'}
                ];

                $scope.labelPositionArray = [
                    {value:'',text:''},
                    {value:'top',text:'上'},
                    {value:'bottom',text:'下'},
                    {value:'left',text:'左'},
                    {value:'right',text:'右'},
                    {value:'insideTop',text:'内上'},
                    {value:'insideBottom',text:'内下'},
                    {value:'insideLeft',text:'内左'},
                    {value:'insideRight',text:'内右'}
                ];

                $scope.currentSeries = undefined;
                $scope.currentBinding = undefined;
                $scope.currentSeriesOption = undefined;

                if(!$scope.extendOfIndex)
                    $scope.extendOfIndex = {};

                if(!$scope.extendOfIndex.SeriesOption)
                    $scope.extendOfIndex.SeriesOption = [];

                if($scope.extendOfIndex.XboundaryGap === undefined){
                    $scope.extendOfIndex.XboundaryGap = false;
                }
                if($scope.extendOfIndex.XboundaryGap === undefined){
                    $scope.extendOfIndex.XboundaryGap = false;
                }

                $scope.addSeries = function () {
                    $scope.currentSeriesOption = {
                        SeriesName:'序列_' + new Date().getSeconds(),
                        DataSetCode:'',
                        DimensionCode:'',
                        MeasureCode:'',
                        SeriesType:'',
                        SeriesStack:'',
                        LabelPosition:'',
                        IsAreaStyle:false
                    };
                    $scope.currentBinding = undefined;
                    $scope.extendOfIndex.SeriesOption.push($scope.currentSeriesOption);
                };

                $scope.removeSeries = function(){
                    if($scope.currentBinding){
                        var index = $scope.extendOfIndex.SeriesOption.indexOf($scope.currentSeriesOption);
                        $scope.extendOfIndex.SeriesOption.splice(index,1)
                        $scope.currentBinding = undefined;
                    }
                };

                $scope.selectedSeries = function () {
                    $scope.currentBinding = _.findWhere($scope.bindingOfIndex,{DataSetCode:$scope.currentSeriesOption.DataSetCode});
                };
                
                $scope.bindingChanged = function (binding) {
                    if($scope.currentSeriesOption)
                        $scope.currentSeriesOption.DataSetCode = binding.DataSetCode;
                };

                $scope.eventSelect = function () {
                    $scope.bindPivotEvent({
                        Event:"SeriesClick",
                        Params:[
                            {Value:"SeriesName",Name:"序列名称"},
                            {Value:"Dimension",Name:"序列维度值"},
                            {Value:"Measure",Name:"序列度量值"}
                        ]
                    });
                };
            }]
        );
    });